package com.sdry.web.controller.llm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sdry.model.llm.LlmCancellingStocks;
import com.sdry.model.llm.LlmCancellingStocksDetail;
import com.sdry.model.zc.ZcInventoryInfoEntity;
import com.sdry.model.zc.ZcRejectsWarehouseEntity;
import com.sdry.service.llm.CancellingStockDetailService;
import com.sdry.service.llm.CancellingStockService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONObject;

/**
 * App接口
 * @Title:AppInterfaces
 * @Package com.sdry.web.controller.llm
 * @author llm
 * @date 2019年5月17日
 * @version 1.0
 */
@Controller
@RequestMapping("/appInterfaces")
public class AppInterfaces {

	@Resource
	private CancellingStockService cancellingStockService;
	@Resource
	private CancellingStockDetailService cancellingStockDetailService;
	
	/**
	 * 查询所有退库单
	 * @Title:appGetCancellingStock  
	 * @param response
	 */
	@RequestMapping("/appGetCancellingStock")
	public void appGetCancellingStock(HttpServletResponse response){
		List<LlmCancellingStocks> list = cancellingStockService.getAllCancellingStock();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("status", "1");
		jsonObject.put("data", list);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 根据退库单号查询详情
	 * @Title:appGetCancellingStockDetailByNumber  
	 * @param cancellingNumber 退库单号
	 * @param response
	 */
	@RequestMapping("/appGetCancellingStockDetailByNumber")
	public void appGetCancellingStockDetailByNumber(String cancellingNumber, HttpServletResponse response){
		List<LlmCancellingStocksDetail> list = cancellingStockDetailService.getCancellingStockDetailByNumber(cancellingNumber);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("status", "1");
		jsonObject.put("data", list);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * app提交的退库单详情
	 * @Title:appSubmitCancellingStockDetail   
	 * @param json
	 * @param response
	 */
	@RequestMapping("/appSubmitCancellingStockDetail")
	public void appSubmitCancellingStockDetail(String json, HttpServletResponse response){
		//json转list集合
		List<LlmCancellingStocksDetail> list = new ArrayList<>();
		Gson gson = new Gson();
		list = gson.fromJson(json, new TypeToken<List<LlmCancellingStocksDetail>>() {}.getType());
		String status = "0";
		String data = "未退库";
		if(list.size() > 0){
			Map<String, Object> map = new HashMap<>();
			LlmCancellingStocks cancellingStock = cancellingStockService.getCancellingStockByNumber(list.get(0).getCancellingNumber());
			Integer source = cancellingStock.getSource();
			Long warehouseId = cancellingStock.getWarehouseId();
			Long customerId = cancellingStock.getClientId();
			for(LlmCancellingStocksDetail detail : list){
				if(detail.getActualQuantity() >= 0 && detail.getId() != null && !"".equals(detail.getId())){
					//修改退库单详情的实际退库数量
					map.put("id", detail.getId());
					map.put("fieldName", "actualQuantity");
					map.put("fieldValue", detail.getActualQuantity());
					int i = cancellingStockDetailService.updateCancellingStockDetail2(map);
					if(i > 0){
						map.put("id", detail.getMaterialId());
						map.put("warehouseId", warehouseId);
						map.put("customerId", customerId);
						if(source == 1){
							//根据物料ID和仓库ID查良品库存数量
							ZcInventoryInfoEntity infoEntity = cancellingStockDetailService.getGoodQuantityByMid(map);
							map.put("mid", detail.getMaterialId());
							map.put("fieldName", "mNum");
							map.put("fieldValue", infoEntity.getmNum() - detail.getActualQuantity());
							//修改库存数量
							cancellingStockDetailService.updateInventory(map);
							//修改拆箱总数
							map.put("fieldName", "unum");
							cancellingStockDetailService.updateUnpacking(map);
						}else{
							//根据物料ID和仓库ID查不良品库存数量
							ZcRejectsWarehouseEntity reject = cancellingStockDetailService.getBadQuantityByMid(map);
							if(reject != null){
								if(reject.getTotality() - detail.getActualQuantity() == 0){
									//删除不良品库存数据
									cancellingStockDetailService.deleteRejectsWarehouse(reject.getId());
								}else{
									map.put("mid", detail.getMaterialId());
									map.put("fieldName", "totality");
									map.put("fieldValue", reject.getTotality() - detail.getActualQuantity());
									//修改不良品库存数据
									cancellingStockDetailService.updateRejectsWarehouse(map);
								}
							}
						}
						//修改退库单详情的状态
						map.put("fieldName", "status");
						map.put("fieldValue", 1);
						cancellingStockDetailService.updateCancellingStockDetail2(map);
					}
				}
			}
			status = "1";
			data = "已退库";
			//查询详情是否都退库
			List<LlmCancellingStocksDetail> detailList = cancellingStockDetailService.getCancellingStockDetailByNumber(list.get(0).getCancellingNumber());
			int sum = 0;
			for(LlmCancellingStocksDetail det : detailList){
				if(det.getStatus() == 1){
					sum += 1;
				}
			}
			//如果详情都退库，修改退库单的状态为已退库
			if(sum == detailList.size()){
				map.put("cancellingNumber", detailList.get(0).getCancellingNumber());
				map.put("fieldName", "status");
				map.put("fieldValue", 1);
				cancellingStockService.updateConcellingStock2(map);
			}
		}
		JSONObject jsonObject= new JSONObject();
		jsonObject.put("status", status);
		jsonObject.put("data", data);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
